﻿package morn.core.components
{
    import morn.editor.core.*;

    public class View extends Container
    {
        public var pluginName:String;
        private var _uiInit:Boolean = false;
        public static var xmlMap:Object = {};
        public static var uiClassMap:Object = {Box:Box, Button:Button, UrlImage:UrlImage, CheckBox:CheckBox, Clip:Clip, ComboBox:ComboBox, Component:Component, Container:Container, FrameClip:FrameClip, HScrollBar:HScrollBar, HSlider:HSlider, Image:Image, Label:Label, LinkButton:LinkButton, List:List, Panel:Panel, ProgressBar:ProgressBar, RadioButton:RadioButton, RadioGroup:RadioGroup, ScrollBar:ScrollBar, Slider:Slider, Tab:Tab, TextArea:TextArea, TextInput:TextInput, View:View, ViewStack:ViewStack, VScrollBar:VScrollBar, VSlider:VSlider, HBox:HBox, VBox:VBox};
        public static var viewClassMap:Object = {};
        private static var _id:int = 0;
        private static const Skin:String = "skin";
        private static const URL:String = "url";
        private static const VScrollBarSkin:String = "vScrollBarSkin";
        private static const ScrollBarSkin:String = "scrollBarSkin";

        public function View() : void
        {
            return;
        }// end function

        protected function createView(param1:Object) : void
        {
            if (param1 is XML)
            {
                createComp(param1 as XML, this, this);
                this.initUI();
            }
            else if (this.pluginName != null)
            {
                App.skinManager.addViewInitHandler(this.pluginName, this.initUI);
                createCompObj(param1, this, this);
            }
            else
            {
                createCompObj(param1, this, this);
                this.initUI();
            }
            return;
        }// end function

        public function initUI() : void
        {
            if (!this._uiInit)
            {
                if (this.pluginName != null)
                {
                    App.skinManager.addToProcess(this.pluginName);
                }
                this._uiInit = true;
                this.initializeView();
            }
            return;
        }// end function

        protected function initializeView() : void
        {
            return;
        }// end function

        protected function loadUI(param1:String) : void
        {
            var _loc_2:* = xmlMap[param1];
            if (_loc_2)
            {
                this.pluginName = App.CurPluginName;
                this.createView(_loc_2);
            }
            return;
        }// end function

        public function reCreate(param1:Component = null) : void
        {
            param1 = param1 || this;
            var _loc_2:* = param1.dataSource;
            if (param1 is Box)
            {
                Box(param1).removeAllChild();
            }
            createCompObj(param1.comXml, param1, this);
            param1.dataSource = _loc_2;
            return;
        }// end function

        public static function createCompObj(param1:Object, param2:Component = null, param3:View = null, param4:Boolean = false, param5:Boolean = false) : Component
        {
            var _loc_6:Object = null;
            var _loc_7:String = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:Object = null;
            var _loc_11:String = null;
            var _loc_12:String = null;
            var _loc_13:ISkinAbleContainer = null;
            param2 = param2 || getCompInstanceObj(param1, param4);
            if (param2.cached)
            {
                return param2;
            }
            param2.comXml = param1;
            if (param1.children)
            {
                _loc_8 = 0;
                _loc_9 = param1.children.length;
                while (_loc_8 < _loc_9)
                {
                    
                    _loc_10 = param1.children[_loc_8];
                    if (param2 is List && _loc_10.attributes && _loc_10.attributes.name == "render")
                    {
                        List(param2).itemRender = _loc_10;
                    }
                    else
                    {
                        param2.addChild(createCompObj(_loc_10, null, param3, param4, param5));
                    }
                    _loc_8++;
                }
            }
            for (_loc_7 in param1.attributes)
            {
                
                _loc_6 = param1.attributes[_loc_7];
                _loc_11 = _loc_7;
                _loc_12 = _loc_6 + "";
                if (param2.hasOwnProperty(_loc_11))
                {
                    if (App.CurPluginName != null && (_loc_11 == URL || _loc_11 == Skin || _loc_11 == VScrollBarSkin || _loc_11 == ScrollBarSkin) && param2 is ISkinAbleContainer)
                    {
                        _loc_13 = param2 as ISkinAbleContainer;
                        _loc_13.skinChanged = false;
                        App.skinManager.skinLater(_loc_13, _loc_12, param5 ? (App.CurPluginName + "list") : (App.CurPluginName));
                    }
                    else
                    {
                        param2[_loc_11] = _loc_12 == "true" ? (true) : (_loc_12 == "false" ? (false) : (_loc_12));
                    }
                    continue;
                }
                if (_loc_11 == "var" && param3 && param3.hasOwnProperty(_loc_12))
                {
                    param3[_loc_12] = param2;
                }
            }
            if (param2 is IItem)
            {
                IItem(param2).initItems();
            }
            if (param2 is List && App.CurPluginName != null)
            {
                (param2 as List).changeNow();
            }
            return param2;
        }// end function

        static function getCompInstanceObj(param1:Object, param2:Boolean) : Component
        {
            var _loc_3:* = param1.runtime;
            var _loc_4:* = Boolean(_loc_3) ? (viewClassMap[_loc_3]) : (uiClassMap[param1.typeName]);
            var _loc_5:* = param2 ? (App.renderItemFactory.getItem(param1, _loc_4)) : (null);
            if (param2 ? (App.renderItemFactory.getItem(param1, _loc_4)) : (null))
            {
                _loc_5.cached = true;
                return _loc_5;
            }
            return _loc_4 ? (new _loc_4) : (null);
        }// end function

        public static function createComp(param1:XML, param2:Component = null, param3:View = null) : Component
        {
            var _loc_6:XML = null;
            var _loc_7:XML = null;
            var _loc_8:String = null;
            var _loc_9:String = null;
            var _loc_10:ISkinAbleContainer = null;
            param2 = param2 || getCompInstance(param1);
            param2.comXml = param1;
            var _loc_4:int = 0;
            var _loc_5:* = param1.children().length();
            while (_loc_4 < _loc_5)
            {
                
                _loc_7 = param1.children()[_loc_4];
                if (param2 is List && _loc_7.@name == "render")
                {
                    List(param2).itemRender = _loc_7;
                }
                else
                {
                    param2.addChild(createComp(_loc_7, null, param3));
                }
                _loc_4++;
            }
            for each (_loc_6 in param1.attributes())
            {
                
                _loc_8 = _loc_6.name().toString();
                _loc_9 = _loc_6;
                if (param2.hasOwnProperty(_loc_8))
                {
                    if (param3 && App.CurPluginName != null && (_loc_8 == URL || _loc_8 == Skin || _loc_8 == VScrollBarSkin || _loc_8 == ScrollBarSkin) && param2 is ISkinAbleContainer)
                    {
                        _loc_10 = param2 as ISkinAbleContainer;
                        _loc_10.skinChanged = false;
                        App.skinManager.skinLater(_loc_10, _loc_9, App.CurPluginName);
                    }
                    else
                    {
                        param2[_loc_8] = _loc_9 == "true" ? (true) : (_loc_9 == "false" ? (false) : (_loc_9));
                    }
                    continue;
                }
                if (_loc_8 == "var" && param3 && param3.hasOwnProperty(_loc_9))
                {
                    param3[_loc_9] = param2;
                }
            }
            if (param2 is IItem)
            {
                IItem(param2).initItems();
            }
            return param2;
        }// end function

        static function getCompInstance(param1:XML) : Component
        {
            var _loc_2:* = param1.@runtime;
            var _loc_3:* = Boolean(_loc_2) ? (viewClassMap[_loc_2]) : (uiClassMap[param1.name()]);
            return _loc_3 ? (new _loc_3) : (null);
        }// end function

        public static function registerComponent(param1:String, param2:Class) : void
        {
            uiClassMap[param1] = param2;
            return;
        }// end function

    }
}
